{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PyTorch: Creating Datasets from AIS Backend\n",
    "In the rapidly evolving field of machine learning, efficient data handling is crucial for training models effectively. This guide explores how to leverage AIStore (AIS), a scalable object storage solution, to create and manage datasets directly within PyTorch. We'll cover the integration of AIS with PyTorch through two custom dataset classes: AISDataset for map-style datasets and AISIterDataset for iterable datasets. These classes facilitate seamless access to data stored in AIS, supporting a variety of machine learning workflows. For details refer to [README](https://github.com/NVIDIA/aistore/tree/main/python/aistore/pytorch).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Imports\n",
    "import os\n",
    "from torch.utils.data import DataLoader\n",
    "from aistore.pytorch import AISMapDataset, AISIterDataset\n",
    "from aistore.sdk import Client"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup client and necessary buckets "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ais_url = os.getenv(\"AIS_ENDPOINT\", \"http://localhost:8080\")\n",
    "client = Client(ais_url)\n",
    "bucket = client.bucket(\"my-bck\").create(exist_ok=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Create some objects in the bucket"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "object_names = [f\"example_obj_{i}\" for i in range(10)]\n",
    "for name in object_names:\n",
    "    bucket.object(name).get_writer().put_content(f\"{name} - object content\".encode(\"utf-8\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating a Map-Style Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "map_dataset = AISMapDataset(ais_source_list=bucket)\n",
    "\n",
    "for i in range(\n",
    "    len(map_dataset)\n",
    "):  # calculate length of all items present using len() function\n",
    "    print(map_dataset[i])  # get object url and byte array of the object\n",
    "\n",
    "# Create a DataLoader from the dataset\n",
    "map_data_loader = DataLoader(map_dataset, batch_size=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating a Iterable-Style Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "iter_dataset = AISIterDataset(ais_source_list=bucket)\n",
    "for sample in iter_dataset:\n",
    "    print(sample)  # get object url and byte array of the object\n",
    "\n",
    "# Create a DataLoader from the dataset\n",
    "iter_data_loader = DataLoader(iter_dataset, batch_size=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Note:** We can also provide an etl_name (which is present in our cluster) to the Dataset to apply an etl to the objects. For example -  `AISMapDataset(ais_source_list=bucket, etl_name=your_etl_name)`"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dev",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
